package cn.linstudy.asserts;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author ljp
 * @Date 2021/10/28 18:33
 * @Description 仿照Spring定制的验证类
 */
public class AssertUtils{

    /**
     ======================需要引入的依赖有======================
     <dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     <version>3.9</version>
     </dependency>
     =========================================================
     */


    public static void state(boolean expression, String message) {
        if (!expression) {
            throw new IllegalStateException(message);
        }
    }

    public static void state(boolean expression, String message, Object... additional) {
        if (!expression) {
            throw new IllegalStateException(message + Arrays.toString(additional));
        }
    }

    public static void isNull(Object object, String message, Object... additional) {
        if (object != null) {
            throw new IllegalArgumentException(message + Arrays.toString(additional));
        }
    }

    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object, String message, Object... additional) {
        if (object == null) {
            throw new IllegalArgumentException(message + Arrays.toString(additional));
        }
    }

    public static void hasLength(String text, String message) {
        if (StringUtils.isEmpty(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasLength(String text, String message, Object... additional) {
        if (StringUtils.isEmpty(text)) {
            throw new IllegalArgumentException(message + Arrays.toString(additional));
        }
    }

    public static void equals(Object o1, Object o2, String message) {
        if (!Objects.equals(o1, o2)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void equals(Object o1, Object o2, String message, Object... additional) {
        if (!Objects.equals(o1, o2)) {
            throw new IllegalArgumentException(message + Arrays.toString(additional));
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (null == collection || collection.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Collection<?> collection, String message, Object... additional) {
        if (null == collection || collection.isEmpty()) {
            throw new IllegalArgumentException(message + Arrays.toString(additional));
        }
    }
}
